package com.example.marketapp.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

import com.example.marketapp.banco.BaseClienteDAO;
import com.example.marketapp.model.Cliente;

public class ClienteDAO {
	
private static final String TABLE_NAME = "cliente";
	
	private Context context;
	private SQLiteDatabase database;
	BaseClienteDAO baseClienteDAO;
	
	public ClienteDAO(Context context) {
	      this.context = context;
	      baseClienteDAO = new BaseClienteDAO(this.context);
	      this.database = baseClienteDAO.getWritableDatabase();
	}
	
	public void open() {
		database = baseClienteDAO.getWritableDatabase();
	}
	
	
	public void close() {
		baseClienteDAO.close();
	}
	
	public int insert(Cliente cliente) {
		ContentValues values = new ContentValues(); 
		values.put("nome", cliente.getNome()); 
        values.put("cpf", cliente.getCpf()); 
        values.put("email", cliente.getEmail());
        values.put("senha", cliente.getSenha());
	      
        return (int) database.insert(TABLE_NAME, null, values);
	}
	
	public int delete (int id_cliente){ 
		return database.delete(TABLE_NAME,"id_cliente = " + id_cliente, null); 
	}
	
	public int update(Cliente cliente){
		
		ContentValues values = new ContentValues(); 
		values.put("nome", cliente.getNome()); 
        values.put("cpf", cliente.getCpf()); 
        values.put("email", cliente.getEmail());
        values.put("senha", cliente.getSenha());
        
        return database.update(TABLE_NAME, values, "id_cliente = " + cliente.getId(), null);
	}
	
	public List<Cliente> getCliente() {

		String[] columns = { "id_cliente", "nome", "cpf", "email", "senha"};
		String selection = null;
		String[] selectionArgs = null;
		String groupBy = null;
		String having = null;
		String orderBy = null;
		String limit = null;

		Cursor cursor = baseClienteDAO.getWritableDatabase().query(TABLE_NAME, columns, selection,
				selectionArgs, groupBy, having, orderBy, limit);

		List<Cliente> cliente = new ArrayList<Cliente>();

		while (cursor.moveToNext()) {
			
			cliente.add(toCliente(cursor));			
		}
		cursor.close();
		return cliente;
	}
	
	
	private Cliente toCliente(Cursor cursor) {
		Cliente cliente = new Cliente();
		cliente.setId(cursor.getInt(0));
		cliente.setNome(cursor.getString(1));
		cliente.setCpf(cursor.getString(2));
		cliente.setEmail(cursor.getString(3));
		cliente.setSenha(cursor.getString(4));
		return cliente;
	}

	public boolean validaLogin(String email,String senha) throws SQLiteException { 
		try {
			
        Cursor cursor = database.rawQuery("select * from "+TABLE_NAME+" where email='" +email+ "'and senha = '"+senha+"'  ", null);
       
        if (cursor.getCount()==0) return false;
       
        return true;
        
		} catch (Exception e) {
			
			e.printStackTrace();
			throw new SQLiteException("Ocorreu um erro ao validar login na base de dados!");
		}
	}
	
	public void deletar(Cliente cliente) {
		baseClienteDAO.getWritableDatabase().delete(TABLE_NAME, "id_cliente=?",
				new String[] { cliente.getId().toString() });
	}
	
	public boolean isCliente(String email) {
		Cursor rawQuery = baseClienteDAO.getReadableDatabase().rawQuery("select email from " + TABLE_NAME + " where email = ?", new String[]{email});
		int total = rawQuery.getCount();
		rawQuery.close();
		baseClienteDAO.close();
		return total > 0;
	}

}
